/*
 * @Author: Drc
 * @Date: 2020-06-11 16:27:52
 * @LastEditors: Drc
 * @LastEditTime: 2021-09-24 10:02:34
 */

import AsyncStorage from '@react-native-community/async-storage'
import store from '@path/redux'

export type ThemeType = 'light' | 'dark'
const THEME_KEY = 'THEME_KEY'
const DEFAULT_THEME: ThemeType = 'light'

interface ThemeStyle {
  text_main: string
  text_minor: string
  text_label: string
  text_disable: string
  text_tip: string
  bg: string
  bg_deep: string
  theme: string
  holder: string
  border: string
  reverse: string
  success: string
  error: string
  warn: string
}

/**
 * 主题Dao
 */
class Theme {
  static currentTheme: ThemeType

  private static Theme_light: ThemeStyle = {
    text_main: '#000000',
    text_minor: '#999999',
    text_label: '#9879CA',
    text_disable: '#DCDCDC',
    text_tip: 'rgba(0,0,0,0.5)',
    bg: '#ffffff',
    bg_deep: '#FCFCFC',
    theme: '#8644FF',
    holder: 'rgba(0,0,0,0.3)',
    border: '#DCDCDC',
    reverse: '#ffffff',
    success: '#08DA69',
    error: '#FF0000',
    warn: '#EAA111',
  }

  private static Theme_dark: ThemeStyle = {
    text_main: '#000000',
    text_minor: '#999999',
    text_label: '#9879CA',
    text_disable: '#DCDCDC',
    text_tip: 'rgba(0,0,0,0.5)',
    bg: '#ffffff',
    bg_deep: '#FCFCFC',
    theme: '#8644FF',
    holder: 'rgba(0,0,0,0.3)',
    border: '#DCDCDC',
    reverse: '#ffffff',
    success: '#08DA69',
    error: '#FF0000',
    warn: '#EAA111',
  }

  private static shadow_light = {
    shadowColor: '#000',
    shadowOffset: {width: 3, height: 3},
    shadowOpacity: 0.12,
    shadowRadius: 8,
    elevation: 3,
  }

  private static shadow_dark = {
    shadowColor: '#000',
    shadowOffset: {width: 3, height: 3},
    shadowOpacity: 0.12,
    shadowRadius: 8,
    elevation: 3,
  }

  /**
   * 获取主题
   */
  static async getTheme() {
    if (this.currentTheme) {
      return this.currentTheme
    }
    try {
      let theme: ThemeType = await AsyncStorage.getItem(THEME_KEY)
      if (theme) {
        this.currentTheme = theme
        return theme
      } else {
        this.currentTheme = DEFAULT_THEME
        return DEFAULT_THEME
      }
    } catch (error) {
      throw new Error(error)
    }
  }

  static getThemeColor(color: keyof ThemeStyle, theme: ThemeType = this.currentTheme) {
    return this[`Theme_${theme}`][color] ?? ''
  }

  /**
   * 设置主题
   * @param theme 主题
   */
  static async setTheme(theme: ThemeType) {
    if (!theme || theme === this.currentTheme) {
      return
    }
    this.currentTheme = theme
    store.dispatch.global.setTheme(theme)
    await AsyncStorage.setItem(THEME_KEY, theme)
  }

  static getShadow(theme: ThemeType = this.currentTheme || DEFAULT_THEME) {
    switch (theme) {
      case 'light':
        return this.shadow_light
      case 'dark':
        return this.shadow_dark
    }
  }
}

export default Theme
